import {Component, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Router} from '@angular/router';
import {NzModalService} from 'ng-zorro-antd';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss']
})
export class ListComponent implements OnInit {
  listOfData: any[] = [];

  constructor(
    private http: HttpClient,
    private router: Router,
    private modal: NzModalService,
  ) {
  }

  ngOnInit() {
    this.loadData();
  }

  loadData() {
    this.http.get<any[]>('/api/environment').subscribe(res => {
      console.log('loadData', res);
      this.listOfData = res;
    });
  }

  detail(id: any) {
    console.log('detail', id);
    this.router.navigate(['/environment/detail', id]);
  }

  add() {
    this.router.navigate(['/environment/add']);
  }

  delete(id: any) {
    console.log('delete', id);
    this.modal.confirm({
      nzTitle: '确认删除吗？',
      nzContent: '删除之后数据无法恢复，请确认。',
      nzOnOk: () => {
        this.http.delete('/api/environment/' + id).subscribe(() => {
          this.loadData();
        });
      }
    });
  }
}
